<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/3
 * Time: 上午8:04
 */

namespace app\api\module;


use app\common\model\DeveloperException;
use app\common\model\User;
use app\common\model\Consortium;
use app\common\model\Withdraw as WithdrawModel;
use app\common\model\UserException;

class Withdraw extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     * @throws DeveloperException
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】申请提现
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName withdraw_apply
             * @apiGroup 提现【Withdraw】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} type 提现方式（1：微信，2：支付宝，3：银行卡）
             * @apiParam (dataGroup) {String} account 提现账号
             * @apiParam (dataGroup) {String} name 账号姓名
             * @apiParam (dataGroup) {String} money 提现金额
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Withdraw",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "type":"1",
             *     "account":"123456",
             *     "name":"李四",
             *     "money":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} withdraw_id 提现编号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "withdraw_id": "1"
             *   },
             *   "result_code": "0000",
             *   "result_info": "线下支付！"
             * }
             *
             */
            case H_WITHDRAW_APPLY:
                return $this->withdraw_apply();
            /**
             * @api {post} / 【1002】提现列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName withdraw_list
             * @apiGroup 提现【Withdraw】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Withdraw",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "category_id":"1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} list 提现列表
             *
             * @apiSuccess (union_list-消息列表) {String} create_time 提现时间
             * @apiSuccess (union_list-消息列表) {String} money 提现金额
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "list": [
             *       {
             *             "create_time":"123456,
             *             "money":"1.23"
             *       },
             *       {
             *             "create_time":"123456,
             *             "money":"1.23"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "提现列表！"
             * }
             *
             */
            case H_WITHDRAW_LIST:
                return $this->withdraw_list();
            default:
                send_developer_error("模块【活动资源】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /**
     * 申请提现
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_apply()
    {
        $access_token=$this->essentialParam('access_token');
        $type=$this->essentialParam('type');
        $account=$this->essentialParam('account');
        $name=$this->essentialParam('name');
        $money=$this->essentialParam('money');

        $user=User::userFromToken($access_token);
        /*联盟商信息*/
        $info=Consortium::accountInfo($user['id']);

        /*生成提现记录*/
        $withdraw_id=WithdrawModel::withdrawApply($user,$info,$type,$account,$name,$money);

        $this->insertResponse('withdraw_id',$withdraw_id);

        return $this->response("申请提现");

    }

    /*
     * 联盟商列表
     * */
    public function withdraw_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $user=User::userFromToken($access_token);

        $list=WithdrawModel::withdrawList($user,$page,$page_size);
        $this->insertResponse('list',$list);
        return $this->response("提现列表！");

    }






}